﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Microsoft.Popfly.GameCreator.GameEngine.Behaviors
{
    abstract public class MoveRelativeToSelfBehavior : XYMoveBehavior
    {
        protected double m_radiansFromForward;
        protected Actor m_actorOwner;
        public MoveRelativeToSelfBehavior(BehaviorOwner owner, double offset, double radiansFromForward)
            : base(owner, offset)
        {
            if (owner.IsActor == false)
            {
                throw new ArgumentException("Moving relative to self is only valid for actors.");
            }
            m_actorOwner = (Actor)owner;
            m_radiansFromForward = radiansFromForward;
        }

        protected void InvokeHelper(out double xResult, out double yResult)
        {
            double rotationRadians = MathUtils.DegreesToRadians(m_actorOwner.Rotation) + m_radiansFromForward;

            MathUtils.RotateRadians(0, -m_offset, rotationRadians, out xResult, out yResult);
        }
    }

    public class AccelerationMoveRelativeToSelf : MoveRelativeToSelfBehavior
    {
        public AccelerationMoveRelativeToSelf(BehaviorOwner owner, double acceleration, double radiansFromForward) : base(owner, acceleration, radiansFromForward) { }



        public override void Invoke(BehaviorOwner sender, Microsoft.Popfly.GameCreator.GameEngine.Events.PGCEventArgs args)
        {
            double xResult, yResult;
            InvokeHelper(out xResult, out yResult);

            m_actorOwner.XAcceleration += xResult;
            m_actorOwner.YAcceleration += yResult;
        }
    }

    public class MaxSpeedMoveRelativeToSelf : MoveRelativeToSelfBehavior
    {
        public MaxSpeedMoveRelativeToSelf(BehaviorOwner owner, double maxSpeed, double radiansFromForward) : base(owner, maxSpeed, radiansFromForward) { }



        public override void Invoke(BehaviorOwner sender, Microsoft.Popfly.GameCreator.GameEngine.Events.PGCEventArgs args)
        {
            double xResult, yResult;
            InvokeHelper(out xResult, out yResult);

            m_actorOwner.XAcceleration += xResult * m_actorOwner.XDrag;
            m_actorOwner.YAcceleration += yResult * m_actorOwner.YDrag;
        }
    }

    public class VelocityMoveRelativeToSelf : MoveRelativeToSelfBehavior
    {
        public VelocityMoveRelativeToSelf(BehaviorOwner owner, double acceleration, double radiansFromForward) : base(owner, acceleration, radiansFromForward) { }



        public override void Invoke(BehaviorOwner sender, Microsoft.Popfly.GameCreator.GameEngine.Events.PGCEventArgs args)
        {
            double xResult, yResult;
            InvokeHelper(out xResult, out yResult);

            m_actorOwner.XVelocity += xResult;
            m_actorOwner.YVelocity += yResult;
        }
    }
}
